var formSubmit = function(winId,formId,url,action,params,callback){
    $('#'+formId).form('submit', {
        url:url,
        dataType:'json',
        onSubmit:function(param){
            if(!$(this).form('validate')){
                return false;
            }
            if(params){
                $.extend(param,params);
            }
            $.messager.progress();
        },
        success:function(obj){
        	obj = $.parseJSON(obj);
        	$.successParser(obj);
            if(obj.success){
            	$('#'+winId).window('close');
            	$.messager.alert('提示',action+'成功！');
            	if(callback){
            		callback();
            	}
            }
            $.messager.progress('close');
        },
        onLoadError:function(){
            $.messager.progress('close');
        }
    });
};
var parseCondition = function(form,combos){
    var condition = [];
    var params = {};
    $(".label",form).each(function(i,v){
        var _for = $(this).attr('for');
        if(_for){
        	var input = $('[name='+_for+']',form);
        	var val = input.val();
        	if(val){
        		var name = input.attr('name');
        		params[name] = val;
        		if($.inArray(name,combos||{}) != -1){
        			val = $('#'+name).combobox('getText');
        		}
        		var text = $(this).text();
        		condition.push({
        			name:name,
        			text:text,
        			value:val
        		});
        	}
        }
    });
    $('#condition').data('params',params);
    var items = $('#items');
    $(".item",items).remove();
    $.each(condition,function(i,v){
        items.prepend('<div class="item"><b>'+v.text+'</b>：'+v.value+'<a title="删除该条件" href="javascript:void(0);" to="'+v.name+'"></a></div>');
    });
    return params;
};
var _detail = function(id){
	var box = $("#detailWin");
	box.show();
	box.dialog({
		href:'detail.fg?id='+id,
		width:400,
		modal:true,
		onLoad:function(){
			$("#detailWin").dialog('center');
		},
		buttons:[{
			text:'关闭',
			handler:function(){
				$("#detailWin").dialog('close');
			}
		}]
	});
};
var _add = function(callback){
	var box = $("#addWin");
	box.show();
	box.dialog({
		href:'add.html',
		width:400,
		modal:true,
		onLoad:function(){
			$("#addWin").dialog('center');
		},
		buttons: [{
			text:'确定',
			iconCls:'icon-ok',
			handler:function(){
				formSubmit('addWin','addForm','insert.jx','新建',{},callback);
			}
		},{
			text:'关闭',
			handler:function(){
				$("#addWin").dialog('close');
			}
		}]
	});
};
var _update = function(id,callback){
	var box = $("#updateWin");
	box.show();
	box.dialog({
		href:'update-page.fg?id='+id,
		width:400,
		modal:true,
		onLoad:function(){
			$("#updateWin").dialog('center');
		},
		buttons: [{
			text:'确定',
			iconCls:'icon-ok',
			handler:function(){
				formSubmit('updateWin','updateForm','update.jx','修改',{id:id},callback);
			}
		},{
			text:'关闭',
			handler:function(){
				$("#updateWin").dialog('close');
			}
		}]
	});
};
var _delete = function(id,callback){
	$.messager.confirm('提示','您确定要删除该项？',function(r){
        if(r){
        	$.messager.progress({msg:'请稍候...'});
            $.ajax({
                url:'delete.jx',
                data:{id:id},
                success:function(obj){
                	$.messager.progress('close');
                	$.successParser(obj);
                    if(obj.success){
                        $.messager.alert('提示','删除成功！','info',function(){
                        	if(callback){
                        		callback();
                        	}
                        });
                    }
                },
                error:function(responseText,statusText){
                	$.messager.progress('close');
                	$.errorParser(responseText,statusText);
                }
            });
        }
    });
};
var _search = function(combos){
	var cond = $("#condWin");
    cond.show();
    cond.dialog({
    	href:'cond.html',
    	width:400,
    	modal:true,
    	onLoad:function(){
			$("#condWin").dialog('center');
		},
        buttons: [{
            text:'确定',
            iconCls:'icon-ok',
            handler:function(){
            	var params = parseCondition($('#condForm'),combos);
                if($.isEmptyObject(params)){
                    $('#condition').hide();
                }else{
                    $('#condition').show();
                }
                $('#list').datagrid('load',params);
                $('#condWin').dialog('close');
            }
        },{
            text:'关闭',
            handler:function(){
            	$("#condWin").dialog('close');
            }
        }]
    });
};
var _delcond = function(calback){
	$("#items").click(function(e){
        var self = $(e.target);
        if(self.is('a')){
        	var cond = $("#condition");
        	var item = self.closest(".item");
        	var to = self.attr('to');
        	var params = cond.data('params')||{};
        	if(to){
        		params[to] = undefined;
        	}
        	if(item.siblings(".item").length == 0){
        		cond.hide();
        	}
        	item.remove();
        	if(calback){
        		calback();
        	}
        }
    });
};
